package com.wxmit.module.system.api;

import cn.hutool.core.collection.ListUtil;
import com.wxmit.framework.satoken.core.util.StpUserUtil;
import com.wxmit.module.system.service.ISysRoleService;
import com.wxmit.system.api.RoleApi;
import com.wxmit.system.domain.SysRoleDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description RoleApiImpl
 * @Author huangxiong
 * @Date 2022/7/12 09:08
 * @Version V1.0
 */
@Service
public class RoleApiImpl implements RoleApi {

    @Autowired
    private ISysRoleService sysRoleService;

    @Override
    public List<String> listPermission(List<String> redisKeys) {
        if(StpUserUtil.isAdmin()){
            return ListUtil.of("*");
        }
        if(CollectionUtils.isEmpty(redisKeys))return Collections.emptyList();
        List<SysRoleDTO> roleCacheValue = sysRoleService.getRoleCacheValue(redisKeys);
        if (CollectionUtils.isEmpty(roleCacheValue))return Collections.emptyList();
        return roleCacheValue.stream().filter(Objects::nonNull).map(SysRoleDTO::getPermissions).flatMap(Collection::stream).collect(Collectors.toList());
    }
}
